// pages/my-order/my-order.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      statusTab: [
        {title: '全部',value: ''},
        {title: '未支付',value: 0},
        {title: '已支付',value: 2},
        {title: '已取消',value: 4},
        {title: '退款中',value: 5},
        {title: '已退款',value: 6}
      ],
      TabCur: 0,

      userInfo: {},
      orderParam: {
        openid: '',
        status: '',
        pageNum: 1
      },
      isLoad: true,
      orderList: [],
      priceFormat: {
        1 : {
          1: '6个月',
          2: '8个月',
          3: '9个月',
          4: '12个月',
        },
        2 : {
          1: '24期',
          2: '36期',
          3: '48期',
          4: '60期',
        }
      }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var orderParam = this.data.orderParam
    orderParam.openid = app.globalData.userInfo.openid
    this.setData({userInfo: app.globalData.userInfo,orderParam:orderParam})
    this.getOrderList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(!this.data.isLoad){
      return false;
    }
    this.getOrderList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },




  getOrderList(){
    var that = this
    wx.request({
      url: app.globalData.apiPath + 'order/list',
      method: 'POST',
      data: that.data.orderParam,
      success (res) {
        console.log(999,res)
        var list = res.data.data.list
        list.forEach((item,index) => {
          item.mainImg = JSON.parse(item.mainImg)
          item.label = JSON.parse(item.label)
       })
       var list = that.data.orderList.concat(list);

        var orderParam = that.data.orderParam ,isLoad = that.data.isLoad
        if(res.data.data.hasNextPage){
            orderParam.pageNum = orderParam.pageNum+1
        }else{
          isLoad = false
        }
        that.setData({orderList:list,orderParam:orderParam ,isLoad:isLoad})
        //wx.showToast({title: '登录成功',icon: 'success',duration: 2000 })
      }
    })
  },
  jumpPage(param){
    wx.showToast({title: '开发中...',icon: 'none',duration: 2000 })
    return false;
    wx.navigateTo({ url: param.currentTarget.dataset.path})
  },
  tabSelect(param){
    var i = param.currentTarget.dataset.index
    var v = param.currentTarget.dataset.value
    var orderParam = this.data.orderParam 
    orderParam.status = v
    orderParam.pageNum = 1
    this.setData({TabCur: i, orderParam: orderParam,orderList: []})
    this.getOrderList()
  },

  wxPay(param){
      var id = param.currentTarget.dataset.id
      var that = this
      wx.request({
        url: app.globalData.apiPath + 'order/wxPaySign',
        method: 'POST',
        data: {id: id},
        success (res) {
          console.log(res)
          if(res.data.code != 0){
              wx.showToast({title: "签名失败",icon: 'error',duration: 2000 }); return false;
          }

          //开始支付
          var payData = res.data.data
          wx.requestPayment(
              {
                'timeStamp': payData.timeStamp+'',
                'nonceStr': payData.nonceStr,
                'package': payData.package,
                'signType': payData.signType,
                'paySign': payData.paySign,
                'success': function (e) {
                  console.log(e)
                  that.updateStatus(id)
                },
                'fail': function (e) { 
                  console.log(e)
                },
                'complete': function (e) {
                  console.log(e)
                }
              }
          )

        }
      })
  },
  updateStatus(id){
    var that = this
    wx.request({
      url: app.globalData.apiPath + 'order/wxPaySuccess',
      method: 'POST',
      data: {id: id},
      success (res) {
        if(res.data.code != 0){
            wx.showToast({title: "状态更新异常",icon: 'error',duration: 2000 }); return false;
        }
        wx.showToast({title: "支付成功",icon: 'none',duration: 2000 })
        
        var orderParam = that.data.orderParam
        orderParam.pageNum = 1
        that.setData({orderParam:orderParam,orderList: [],isLoad: true})
        that.getOrderList()
      }
    })
},

})